home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekikoh Dennoh Club 5
/
Gekikoh Dennoh Club Vol. 5 (Japan).7z
/
Gekikoh Dennoh Club Vol. 5 (Japan) (Track 01).bin
/
internet
/
tcppack
/
tcppackb.lzh
/
src
/
samples
/
arp.c
next >
Wrap
C/C++ Source or Header
|
1994-08-17
|
6KB
|
307 lines
/*
* arp.c
*
* Copyright (C) 1994 Tomoaki Tada/F.C.T.
*/
static char *rcsid = "$Id: arp.c,v 1.6 1994/08/17 09:54:31 Niggle Exp $";
/*
* $Log: arp.c,v $
* Revision 1.6 1994/08/17 09:54:31 Niggle
* correct spell miss.
*
* Revision 1.5 1994/08/04 05:00:48 Niggle
* inetdé¬ÅφÆôé╡é─éóé╚éóÅΩìçé╠ò\Īé≡ò╧ìX
*
* Revision 1.4 1994/08/03 01:57:27 Niggle
* alias é≡Ägùpé╖éΘé╠é≡éΓé▀é─üCnameé⌐éτdomain nameé≡Å£éóé╜éαé╠é≡Ägéñéµéñò╧ìX
*
* Revision 1.3 1994/07/19 03:32:50 Niggle
* gethostbyname()ôÖé≡Ägùpé╖éΘéµéñé╔ò╧ìX
*
* Revision 1.2 1994/05/13 05:44:13 Niggle
* chenged to be able to handle hardware addresses, that is not ethernet address.
*
* Revision 1.1 1994/05/10 09:21:14 Niggle
* Initial revision
*
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"network.h"
#include"socket.h"
static void display_all_table (void);
static void add_a_table (char *, char *);
static void delete_a_table (char *);
static void print_a_table (char *);
/************************************************
* *
************************************************/
void
main (int argc, char **argv)
{
long version = _get_version ();
if (version == -1)
fprintf (stderr, "TCP/IP driveré═ÅφÆôé╡é─éóé▄é╣é±\n");
else
{
int c;
opterr = 1;
c = getopt (argc, argv, "asd:f:");
if (c != EOF)
{
switch (c)
{
case 'a':
/* display all table */
display_all_table ();
break;
case 's':
/* set table */
if (optind + 1 < argc)
add_a_table (argv[optind], argv[optind + 1]);
break;
case 'd':
/* delete table */
if (optarg)
delete_a_table (optarg);
break;
default:
break;
}
}
else
{
/* no opt */
if (argc > 1)
{
/* display table coresspond to host */
print_a_table (argv[1]);
}
else
{
printf ("usage: arp hostname\n");
printf (" arp -a\n");
printf (" arp -d hostname\n");
printf (" arp -s hostname hardware-address\n");
}
}
}
return;
}
/************************************************
* *
************************************************/
void
print_a_table (char *src)
{
long ip;
char buff[32], *p, *hostname;
char ip_addr[32];
struct hostent *h;
if (!isipaddr (src))
{
h = gethostbyname (src);
if (h)
ip = *(long *)h->h_addr;
}
else
{
ip = a2n_ipaddr (src);
h = gethostbyaddr ((char *)&ip, sizeof (long), AF_INET);
if (h)
{
char *q, *r;
q = malloc (strlen (h->h_name) + 1);
if (!q)
hostname = "?";
else
{
strcpy (q, h->h_name);
r = strchr (q, '.');
if (r)
{
if (!strcmp (r + 1, get_domain_name ()))
*r = '\0';
}
hostname = q;
}
}
else
hostname = "?";
}
if (ip)
{
n2a_ipaddr (ip, ip_addr);
p = search_arp_table (ip, 32, buff);
if (p)
{
printf ("%s (%s) at %s\n", src, ip_addr, buff);
}
else
{
/* no-entry */
printf ("%s (%s) -- no entry\n", src, ip_addr);
}
}
else
{
/* unknown host */
printf ("arp: %s: Host name look up failure\n", src);
}
return;
}
/************************************************
* *
************************************************/
static void
display_all_table (void)
{
long *addrs;
int len, i;
addrs = get_arp_array (&len);
if (!addrs)
return;
for (i = 0; i < len; i++)
{
char *hostname;
char buff[32];
char ip_addr[32];
long ip;
struct hostent *h;
ip = addrs[i];
h = gethostbyaddr ((char *)&ip, sizeof (long), AF_INET);
if (h)
{
char *q, *r;
q = malloc (strlen (h->h_name) + 1);
if (!q)
hostname = "?";
else
{
strcpy (q, h->h_name);
r = strchr (q, '.');
if (r)
{
if (!strcmp (r + 1, get_domain_name ()))
*r = '\0';
}
hostname = q;
}
}
else
hostname = "?";
n2a_ipaddr (ip, ip_addr);
search_arp_table (ip, 32, buff);
if (!hostname)
hostname = "?";
printf ("%s (%s) at %s\n", hostname, ip_addr, buff);
}
free (addrs);
return;
}
/************************************************
* *
************************************************/
static void
add_a_table (char *ipaddr, char *hwaddr)
{
char buff[256];
long ip;
route *rt;
iface *IF;
if (!isipaddr (ipaddr))
{
struct hostent *h;
h = gethostbyname (ipaddr);
if (h)
ip = *(long *)h->h_addr;
else
ip = 0;
}
else
ip = a2n_ipaddr (ipaddr);
if (!ip)
{
printf ("arp: %s -- Host name look up failure\n", ipaddr);
return;
}
rt = rt_lookup (ip);
if (!rt || rt->gateway || !rt->iface)
{
printf ("arp: %s -- Direct route dosen't exsists\n", ipaddr);
return;
}
IF = rt->iface;
if (IF->sscan)
{
if (IF->sscan (hwaddr, buff))
{
printf ("arp: %s Invalid hardware address\n", hwaddr);
return;
}
}
else
{
printf ("arp: Unable to convert hardware address,\n"
" because iface can't handle it.\n");
return;
}
add_arp_table (ip, buff);
return;
}
/************************************************
* *
************************************************/
static void
delete_a_table (char *ipaddr)
{
long ip;
if (!isipaddr (ipaddr))
{
struct hostent *h;
h = gethostbyname (ipaddr);
if (h)
ip = *(long *)h->h_addr;
else
ip = 0;
}
else
ip = a2n_ipaddr (ipaddr);
if (ip)
delete_arp_table (ip);
else
printf ("arp: %s -- Host name look up failure\n", ipaddr);
return;
}